// Version 15.2
trigger CaseFromYPTrigger on Case (before insert) 
{
	if(trigger.isBefore){
		if(SilCustomerHelper.getIsFirstBefore()){
			SilCustomerHelper.setIsFirstBeforeToFalse();
					    		        
		    List<Case> casesToFindAccountByAFCustomerID = new List<Case>();
		     
		    Set<string> afCustomerIDs = new Set<string>();
		        
		    string tempPhoneHash;
		    
		    for(Case caseObject : Trigger.new) 
		    {
		    	       
		        if(caseObject.AccountID == null && caseObject.Origin == 'Web' ) 
		        {
					// Report Abuse cases
		            if (null != caseObject.Web_CMR_AFCustomerID__c && RecordTypeHelper.GetRecordTypeID('Case','IOG Incident') == caseObject.RecordTypeId)
		            {               
	                	// Find the account for these cases by AF Customer ID
	                	casesToFindAccountByAFCustomerID.add(caseObject);           
		                afCustomerIDs.add(caseObject.Web_CMR_AFCustomerID__c);
		            }   
		        }   
		    }
		
		    SilCustomerHelper.FindResult findResult;
		    Map<string,SilCustomerHelper.FindResult> results;
		    		
		    // Find Account by Afrontier Customer ID     
		    if(!afCustomerIDs.isEmpty())
		    {
		        results = SilCustomerHelper.FindAccountByAFrontierCustomerId(afCustomerIDs);    
		        for(Case caseObject : casesToFindAccountByAFCustomerID) 
		        {  
		            findResult = results.get(caseObject.Web_CMR_AFCustomerID__c);
		            if(findResult != null && findResult.Count == SilCustomerHelper.ResultCount.One)
		            {
		                caseObject.AccountID = findResult.ID;
		            }
		        }
		    }
		}
	}
}